package com.baosight.sczx.zm.cm.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.zm.cm.domain.ZMCMExportApplication;
import com.baosight.sczx.zm.cm.domain.ZMCMExportApplicationDetail;

public class ServiceZMCM83 extends ServiceEPBase {
	
	/**
	 * 初始化数据
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM80</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-06-21下午08:44:45 张坤
	 * </p>
	 *
	 */
	public EiInfo initLoad(EiInfo inInfo) 
	{
		EiInfo outInfo = this.query(inInfo);
		outInfo.addBlock(generateStatus());
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cm.department", "orgCode", "orgName", false));
		return outInfo;
	}
	
	/**
	 * 主表数据查询方法
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM80</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-06-21 上午09:38:09  张坤  Create
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) 
	{
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		if(inInfo.getBlock(EiConstant.queryBlock) == null)
		{
			inInfo.addBlock(EiConstant.queryBlock);
		}
		inInfo.getBlock(EiConstant.queryBlock).setCell(0, "fdApplicationNotStatus", "草拟");
		return super.query(inInfo, "ZMCMExportApplication.query", new ZMCMExportApplication());
	}
	
	/**
	 * 
	 * <p>Function: queryBaseNum</p>
	 * <p>Description: 查询是否有库存为0的物资</p>
	 * <p>return value:EiInfo</p>
	 * <p>History:
	 *    2012-3-28下午01:44:43 wangkun
	 * </p>
	 *
	 */
	public EiInfo queryBaseNum(EiInfo inInfo)
	{
		String noteCode = "";
		for(int i=0; i<inInfo.getBlock(EiConstant.resultBlock).getRowCount(); i++)
		{
			String fdKeyId = inInfo.getBlock(EiConstant.resultBlock).getCellStr(i, "fdKeyId");
			Map inMap = new HashMap();
			inMap.put("fdApplicationId", fdKeyId);
			List list = dao.query("ZMCMExportApplicationDetail.query", inMap);
			for(int j=0; j<list.size(); j++)
			{
				ZMCMExportApplicationDetail detail = (ZMCMExportApplicationDetail)list.get(j);
				if(detail.getFdNum() == Double.parseDouble("0"))
				{
					noteCode += inInfo.getBlock(EiConstant.resultBlock).getCellStr(i, "fdApplicationCode") + ";";
					break;
				}
			}
		}

		if(!noteCode.equals(""))
		{
			inInfo.setStatus(-1);
			inInfo.setMsg("单据(" + noteCode + ")中有库存为0的物资，是否要审批通过该单据?");
		}
       
		return inInfo;
	}
	
	/**
	 * 	审批数据
	 * <p>Function: Approve</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2010-12-25下午04:51:27 张坤
	 * </p>
	 *
	 */
	public EiInfo Approve(EiInfo inInfo) 
	{
		for(int i=0; i<inInfo.getBlock(EiConstant.resultBlock).getRowCount(); i++)
		{
			inInfo.getBlock(EiConstant.resultBlock).setCell(i, "fdAuditor", UserSession.getUserId());
		}
		EiInfo _temp = super.update(inInfo, "ZMCMExportApplication.approve");
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
       
		return outInfo;
	}
	
	/**
	 * 	退回数据
	 * <p>Function: returnApplication</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-8-3下午04:51:27王堃
	 * </p>
	 *
	 */
	public EiInfo returnApplication(EiInfo inInfo) 
	{
		EiInfo _temp = super.update(inInfo, "ZMCMExportApplication.returnApplication");
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
       
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: generateStatus</p>
	 * <p>Description: </p>
	 * <p>return value:EiBlock</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-3-28上午11:08:36 wangkun
	 * </p>
	 *
	 */
	private EiBlock generateStatus()
	{
		EiBlock block = new EiBlock("cmes.cm.applicationStatus");
		EiColumn column = new EiColumn("value");
		column.setDescName("值");
		block.getBlockMeta().addMeta(column);
		
		Map inMap = new HashMap();
		inMap.put("value", "");
		block.addRow(inMap);
		
		inMap = new HashMap();
		inMap.put("value", "上报");
		block.addRow(inMap);
		
		inMap = new HashMap();
		inMap.put("value", "已审批");
		block.addRow(inMap);
		
		return block;
	}
}
